package com.xhtranslate.service.youdao.controller;

import com.xhtranslate.common.dto.ImageFormDTO;
import com.xhtranslate.common.dto.TextFormDTO;
import com.xhtranslate.common.vo.ResponseEnum;
import com.xhtranslate.common.vo.ResponseVO;
import com.xhtranslate.service.youdao.service.YoudaoTranslateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Map;

/**
 * @author liuxuhuang
 * @version 1.0
 * @package com.xhtranslate.service.youdao.controller
 * @date 2022/11/17 15:10:28
 * @description 通用翻译服务接口
 */
@RestController
@Api(value = "有道翻译请求接口", tags = "有道翻译请求接口")
@RequestMapping(value = "youdaoTranslate")
@Slf4j
public class YoudaoTranslateController {
    @Autowired
    private YoudaoTranslateService youdaoTranslateService;

    @PostMapping(value = "textTranslate")
    @ApiOperation(value = "文本翻译")
    public ResponseVO commonTranslate(@RequestBody @Validated TextFormDTO textFormDTO) throws IOException {
        log.warn("有道==>通用翻译：" + textFormDTO);
        Map map = youdaoTranslateService.commonTranslate(textFormDTO);
        if (map.isEmpty()) {
            return ResponseVO.exception(ResponseEnum.PARAMEXCEPTION);
        }
        return ResponseVO.succ(map);
    }

    @PostMapping(value = "imageTranslate")
    @ApiOperation(value = "图片翻译")
    public ResponseVO imageTranslate(@Validated ImageFormDTO dto) throws IOException {
        log.warn("图片翻译：" + dto);
        Map map = youdaoTranslateService.imageTranslate(dto);
        if (!"0".equals(map.get("errorCode"))) {
            return ResponseVO.exception(map);
        }
        return ResponseVO.succ(map);
    }
}
